package com.veal98.bactrack;

import java.util.ArrayList;
import java.util.List;

public class LC46 {

    private List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        if (nums == null || nums.length == 0) {
            return res;
        }

        List<Integer> track = new ArrayList<>();
        backtrack(nums, track);
        return res;
    }

    private void backtrack(int[] nums, List<Integer> track) {
        // 结束条件
        if (track.size() == nums.length) {
            res.add(new ArrayList(track));
            return ;
        }

        // 遍历选择列表
        for (int i = 0; i < nums.length; i ++) {
            // 剪枝
            if (track.contains(nums[i])) {
                continue;
            }

            // 做选择
            track.add(nums[i]);

            // 进入下一层解空间树
            backtrack(nums, track);

            // 回溯
            track.remove(track.size() - 1);
        }
    }
}
